package com.zw.pdm.module.est.service;

import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.entity.est.CardDetail;
import com.zw.pdm.module.craft.repository.CraftMakeSheetRepository;
import com.zw.pdm.module.est.mapper.CardDetailMapper;
import com.zw.pdm.module.est.mapper.CardMapper;
import com.zw.pdm.module.est.model.CraMakeSheetModel;
import com.zw.pdm.module.est.repository.CardRepository;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @Description:
 *
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class CardCheckService {

    @Autowired
    private CardRepository cardRepository;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CardDetailMapper cardDetailMapper;
    @Autowired
    private LogService logService;
    @Autowired
    private CraftMakeSheetRepository craftMakeSheetRepository;

    /** 列表 */
    public List<CraMakeSheetModel> findWithCardCheck(CraMakeSheet craMakeSheet) {
        craMakeSheet.setCheckNames(ShiroUtils.getLoginName());
        return cardMapper.findWithCardCheck(craMakeSheet);
    }

    /** 子列表查询 */
    public List<Card> findSlaveWithCardCheck(Card card) {
        return cardMapper.findSlaveWithCardCheck(card);
    }

    /** 按主键查询 */
    public Card getCardById(Integer id) {
        return cardRepository.getOne(id);
    }

    /** 查询工艺卡明细 */
    public List<CardDetail> findCardDetailByCardId(Integer id) {
        return cardDetailMapper.findByCardId(id);
    }

    /** 校对通过 */
    @Transactional(rollbackFor = Exception.class)
    public int success(CraMakeSheetModel craMakeSheetModel) {
        logService.saveLog("工艺卡审核同意", null);
        // 修改craReleaseDetail 的isSuccess字段
        cardMapper.updateCraReleaseDetailIsSuccessById(craMakeSheetModel.getBid(), 1);
        // 判断 makesheet 表是否满足审核通过条件，从表isSuccess全为1。
        Double num = cardMapper.findAvgIsSuccessByMakeId(craMakeSheetModel.getId());
        String name = ShiroUtils.getUser().getName();
//        CraMakeSheet craMakeSheet = craftMakeSheetRepository.getOne(craMakeSheetModel.getId());
        CraMakeSheet craMakeSheet = cardMapper.findCraMakeSheet(craMakeSheetModel.getId());
        if (craMakeSheet.getCheckNames() == null) {
            craMakeSheet.setCheckNames(name);
        } else {
            if (!craMakeSheet.getCheckNames().contains(name)) {
                craMakeSheet.setCheckNames(craMakeSheet.getCheckNames() + "," + name);
            }
        }
        craftMakeSheetRepository.saveAndFlush(craMakeSheet);
        // 修改 工艺卡接收后再次变更时的接收状态
        cardMapper.updateCraCardSheetStatus(craMakeSheet.getId());

        if (num != null && num == 1) {
            // 修改 工艺卡 状态 和 下达时间
            //cardMapper.updateStatusAndNotifyTimeByCraMakeSheetId(craMakeSheetModel.getId(), 5, new Date());
            cardMapper.updateCraSheetStatusByCraMakeSheetId(craMakeSheetModel.getId(), 5);
            cardMapper.updateCraSheetNotifyTimeByCraMakeSheetId(craMakeSheetModel.getId(), new Date());
            cardMapper.updateCraMakeSheetNotifyById(craMakeSheetModel.getId(),1, new Date());
            cardMapper.updateCraMakeSheetChangeNotify(craMakeSheetModel.getId(), 1);
        }
        return 1;
    }

    /** 驳回 */
    @Transactional(rollbackFor = Exception.class)
    public int reject(Integer id) {
        logService.saveLog("工艺卡审核驳回", null);
        // 驳回 先清除所有同意的状态
        cardMapper.updateMakeSheetCheckNamesById(id);
        cardMapper.updateReleaseDetailIsSuccessByMakeSheetId(id);
        // 删除工时表数据
        cardMapper.deleteWorkTimeByMakeSheetId(id);

        cardMapper.updateStatusAndCommitTimeByCraMakeSheetId(id, 3, null);
        cardMapper.updateCraMakeSheetChangeCommit(id, 0);
        return cardMapper.updateMakeSheetIsCommitById(id, 1);
    }
}
